/**
 * Copyright (C) 2014-2019 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * <p>
 * Any modifications to this file must keep this entire header intact.
 * <p>
 * Change History - most recent changes go on top of previous changes
 * <p>
 * DDBEntry.java
 * <p>
 * Who   Date       Description
 * ====  =========  =================================================================
 * WY    24Jul2015  initial creation
 */

package com.symaster.common.pixy.meta.adobe;

import com.symaster.common.pixy.io.ReadStrategy;
import com.symaster.common.pixy.meta.MetadataEntry;
import com.symaster.common.pixy.string.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Building block for DDB
public class DDBEntry {
    // Obtain a logger instance
    private static final Logger LOGGER = LoggerFactory.getLogger(DDBEntry.class);
    protected byte[] data;
    protected ReadStrategy readStrategy;
    private final int type;
    private final int size;

    public DDBEntry(DataBlockType etype, int size, byte[] data, ReadStrategy readStrategy) {
        this(etype.getValue(), size, data, readStrategy);
    }

    public DDBEntry(int type, int size, byte[] data, ReadStrategy readStrategy) {
        this.type = type;
        if (size < 0) throw new IllegalArgumentException("Input size is negative");
        this.size = size;
        this.data = data;
        if (readStrategy == null) throw new IllegalArgumentException("Input readStrategy is null");
        this.readStrategy = readStrategy;
    }

    public void print() {
        DataBlockType etype = getTypeEnum();
        if (etype != DataBlockType.UNKNOWN)
            LOGGER.info("Type: {} ({})", etype, etype.getDescription());
        else
            LOGGER.info("Type: Unknown (value 0x{})", Integer.toHexString(type));
        LOGGER.info("Size: {}", size);
    }

    public int getType() {
        return type;
    }

    public DataBlockType getTypeEnum() {
        return DataBlockType.fromInt(type);
    }

    protected MetadataEntry getMetadataEntry() {
        //
        DataBlockType eType = DataBlockType.fromInt(type);

        if (eType == DataBlockType.UNKNOWN) {
            return new MetadataEntry("UNKNOWN [" + StringUtils.intToHexStringMM(type) + "]:", eType.getDescription());
        } else {
            return new MetadataEntry("" + eType, eType.getDescription());
        }
    }

    public int getSize() {
        return size;
    }

    public byte[] getData() {
        return data.clone();
    }
}
